#!/bin/bash

#---------------------------------------------------------------#
# RU: Пожелания и правки присылайте на hash.null @acetone       #
# EN: Send wishes and corrections to the hash.null @acetone     #
#                                                               #
# SimpleYggGen v2.4, acetone, 2020. Russia.                     #
#---------------------------------------------------------------#

#-----------------КОНФИГУРАЦИЯ / CONFIGURATION------------------#
# Set Yggdrasil binary path:
# for Linux:
YGGBIN=/usr/bin/yggdrasil
# for Windows (running that script with MSYS2 (msys2.org):
#YGGBIN="/c/Program Files/Yggdrasil/yggdrasil.exe"
#
# To enable non-special names uncomment that line:
#nonspecial=true
#
# To enable system testing uncomment that line:
runtest=true
# 
#------------------------КОНЕЦ / END----------------------------#

#---------------------------------------------------------------#
#--ЛЮБЫЕ ИЗМЕНЕНИЯ В КОДЕ НИЖЕ ВЫ ДЕЛАЕТЕ НА СВОЙ СТРАХ И РИСК--#
#-----ANY CHANGES IN THE CODE BELOW YOU DO AT YOUR OWN RISK-----#
#---------------------------------------------------------------#

test () {
  timestart=$(date '+%s')

  for (( c=1; c<101; c++ )); do
    tempconf=$("$YGGBIN" -genconf)
    EncryptionPublicKey=$(echo $tempconf | grep -w -o -E "EncryptionPublicKey: [0-9a-f]{64}")
    if [[ $c = 102 ]]; then
      echo '' > /dev/null
    fi
    echo -n -e "\e[36m  Analyzing $c%\e[0m" \\r
  done

  timestop=$(date '+%s')
  final=$(($timestop-$timestart))

  if [[ "$final" -le "1" ]]; then
    color="\e[36m"
  elif [[ "$final" -le "2" ]]; then
    color="\e[32m"
  elif [[ "$final" -le "3" ]]; then
    color="\e[33m"
  else
    color="\e[31m"
  fi

  echo -n -e "$color  Testing taken $final second(s)\e[0m\n\n"
}

log () {
  echo -e "Yggdrasil addresses list\\n$(date '+%Y-%m-%d %H:%M:%S')"\\n > ./syg.log

  if ( ls ./sygX* 2> /dev/null | grep 2 &> /dev/null ); then
    ls ./sygX* 2> /dev/null | tr -d syg | tr _ : | tr -d X >> ./syg.log 2> /dev/null
    echo '' >> ./syg.log
    liness=$(wc ./syg.log -l | cut -f1 -d' ' 2> /dev/null);
    tail -n $(($liness-2)) ./syg.log 2> /dev/null
    echo -e "\e[32m  Names saved to syg.log\e[0m\n"
  else
    echo -e "\e[31m  sygX* file not exist\e[0m\n"
    main
    exit 0
  fi
}

tars () {
  log
  tar -cf ./syg$(date '+%Y%m%d_%H%M').tar ./sygX* syg.log 2> /dev/null
  echo -e "\e[32m  All addresses archived\e[0m\n"
}

intro () {
  clear
  echo -e "\e[38m"' '
  echo -e "  ▒▒▒▒▒▒ ▒    ▒ ▒▒▒▒▒▒"
  sleep 0.05
  echo -e "  ▒      ▒    ▒ ▒"
  sleep 0.05
  echo -e "  ▒▒▒▒▒▒ ▒▒▒▒▒▒ ▒  ▒▒▒"
  sleep 0.05
  echo -e "       ▒      ▒ ▒    ▒"
  sleep 0.05
  echo -e "  ▒▒▒▒▒▒ ▒▒▒▒▒▒ ▒▒▒▒▒▒\e[0m"
  echo -e "\e[38m\n  © SimpleYggGen v2.4\n    acetone, 2020\e[0m\n"
}

main () {
  circle=0
  randomfortune=0
  sleep 0.03
  echo -e -n '  Target'"$color » \e[0m"
  read userinput

  if [[ $userinput = '' ]]; then
    target=::
  elif [[ $userinput == [lL][oO][gG] ]]; then
    log
    sleep 0.3
    main
  elif [[ $userinput == [tT][aA][rR] ]]; then
    tars
    sleep 0.3
    main
  elif [[ $userinput == [tT][aA][rR][dD][eE][lL] ]]; then
    tars
    rm -r ./sygX* 2> /dev/null
    echo -e "\e[33m  Original files deleted\e[0m\n"
    sleep 0.3
    main
  elif [[ $userinput == *[g-zG-Zа-яА-Я\;\+\&\#\~\_\%\=\/]* ]]; then
    sleep 0.03
    echo -e "  \e[31mIncorrect input\e[0m" \\n
    sleep 0.03
    main
  else
    target=$userinput
  fi

  if [[ $nonspecial == "true" ]]; then
    dirname=$(echo $target | tr \* S | tr \| I | tr : C | tr ^ U | tr $ D | tr - M | tr [ L | tr ] J | tr . T)
  else
    dirname=$target
  fi

  clear
  echo -e \\n"  [$color$target\e[0m] $(date '+%Y.%m.%d %H:%M:%S')\n"
  tput civis
  for ((;;)); do
    tempconf=$("$YGGBIN" -genconf)
    EncryptionPublicKey=$(echo $tempconf | grep -w -o -E "EncryptionPublicKey: [0-9a-f]{64}")

    if ( echo $EncryptionPublicKey | "$YGGBIN" -address -useconf | grep -i -E "$target" &> /dev/null ); then
      EncryptionPrivateKey=$(echo $tempconf | grep -w -o -E "EncryptionPrivateKey: [0-9a-f]{64}")
      yggsubnet=$(echo $EncryptionPublicKey | "$YGGBIN" -subnet -useconf)
      yggaddress=$(echo $EncryptionPublicKey | "$YGGBIN" -address -useconf)

      mkdir ./sygX$dirname &> /dev/null
      sygnamefile=$(echo $yggaddress | tr : _)
      echo -e "# Address: $yggaddress\n# Subnet:  $yggsubnet\n\n# Change it to yggdrasil.conf\n  $EncryptionPublicKey\n  $EncryptionPrivateKey\n\n# SimpleYggGen v2.4 [$(date '+%Y-%m-%d %H:%M:%S')] \n# Check new version: https://cloud.disroot.org/s/TA63PBsQ6Z7dfGW" > ./sygX$dirname/$sygnamefile
      randomfortune=$(($randomfortune+1))
    fi

    circle=$(($circle+1))
    echo -n -e "  \e[35m$circle/\e[0m$randomfortune \e[32m$yggaddress\e[0m " \\r
  done
}

# Start script execution
intro

# Run test if enabled
if [[ $runtest == "true" ]]; then
  test
else
  color="\e[37m"
fi

# Start interactive
main